package vn.co.thn.android.videohot.activity;

import vn.co.thn.android.serviceconnect.callback.NetWorkCallBack;
import vn.co.thn.android.serviceconnect.callback.RequestProcessCallBack;
import vn.co.thn.android.videohot.R;
import vn.co.thn.android.videohot.VHConstants;
import vn.co.thn.android.videohot.common.VHCommonFragment;
import vn.co.thn.android.videohot.fragment.VHCategoryFragment;
import vn.co.thn.android.videohot.fragment.VHSearchResultFragment;
import vn.co.thn.android.videohot.fragment.VHTimeLineFragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;
import com.startapp.android.publish.splash.SplashConfig;
import com.startapp.android.publish.splash.SplashConfig.Theme;

public class VHMainActivity extends SlidingFragmentActivity implements OnClickListener, RequestProcessCallBack, NetWorkCallBack {
	private ProgressDialog progressDialog;
	private SlidingMenu mSlidingMenu;
	// private AdView mAdView;
	//

	/** StartApp Native Ad declaration */
	private FrameLayout content;
	private LinearLayout network_error_bg, network_message;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		StartAppSDK.init(this, getString(R.string.start_dev), getString(R.string.start_id), true);

		progressDialog = new ProgressDialog(this);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		progressDialog.setMessage("loading...");

		setContentView(R.layout.vh_main_activity);

		setBehindContentView(R.layout.vh_left_menu);
		mSlidingMenu = getSlidingMenu();
		mSlidingMenu.setBehindOffsetRes(R.dimen._40dp);
		mSlidingMenu.setMode(SlidingMenu.LEFT);
		mSlidingMenu.setBehindScrollScale(0f);
		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		mSlidingMenu.findViewById(R.id.home).setOnClickListener(this);
		mSlidingMenu.findViewById(R.id.search).setOnClickListener(this);
		mSlidingMenu.findViewById(R.id.upload).setOnClickListener(this);
		findViewById(R.id.menu_bar).setOnClickListener(this);
		findViewById(R.id.category).setOnClickListener(this);
		content = (FrameLayout) findViewById(R.id.content);
		network_error_bg = (LinearLayout) findViewById(R.id.network_bg);
		network_error_bg.setOnClickListener(this);
		network_message = (LinearLayout) findViewById(R.id.network_message);
		network_message.setOnClickListener(this);
		replaceFragmentNoBackStackWithAnimation(VHTimeLineFragment.getInstance());
		 StartAppAd.showSplash(this, savedInstanceState, new
		 SplashConfig().setTheme(Theme.SKY).setLogo(R.drawable.ic_launcher).setAppName("Start Video Hot"));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.menu_bar:
			showMenu();
			break;
		case R.id.network_bg:
		case R.id.network_message:
			Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.content);
			if (fragment != null) {
				((VHCommonFragment) fragment).onDataRefresh();
			}

			break;

		case R.id.home:
			mSlidingMenu.showContent();
			replaceFragmentNoBackStackWithAnimation(VHTimeLineFragment.getInstance());
			break;
		case R.id.search:
			mSlidingMenu.showContent();
			startActivityForResult(new Intent(getApplicationContext(), VHSearchActivity.class), VHConstants.REQUEST_CODE_SEARCH);
			break;
		case R.id.upload:
			mSlidingMenu.showContent();
			startActivity(new Intent(getApplicationContext(), VHChooseVideoActivity.class));
			break;
		case R.id.category:
			mSlidingMenu.showContent();
			replaceFragmentNoBackStackWithAnimation(VHCategoryFragment.getInstance());
			break;
		default:
			break;
		}
	}

	@Override
	public void ShowProcess() {
		// TODO Auto-generated method stub
		progressDialog.show();
	}

	@Override
	public void HideProcess() {
		// TODO Auto-generated method stub
		progressDialog.dismiss();
	}

	// replace Fragment
	protected void replaceFragmentNoBackStackWithAnimation(Fragment fragment) {
		final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.setCustomAnimations(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit, R.anim.fragment_slide_left_enter,
				R.anim.fragment_slide_right_exit);
		transaction.replace(R.id.content, fragment);
		transaction.commit();
	}

	// replace Fragment whith animation
	protected void replaceFragmentWithAnimation(Fragment fragment) {
		final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.setCustomAnimations(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit, R.anim.fragment_slide_left_enter,
				R.anim.fragment_slide_right_exit);
		transaction.replace(R.id.content, fragment);
		transaction.addToBackStack(fragment.getClass().getName());
		transaction.commit();
	}

	@Override
	public void NetWorkError(boolean showEmty) {
		// TODO Auto-generated method stub
		if (showEmty) {
			if (network_error_bg.getVisibility() == View.INVISIBLE) {
				network_error_bg.setVisibility(View.VISIBLE);
				// content.setVisibility(View.GONE);
				content.setVisibility(View.INVISIBLE);
			}
		} else {
			if (network_message.getVisibility() == View.INVISIBLE) {
				network_message.setVisibility(View.VISIBLE);
			}
		}
	}

	@Override
	public void NetWorkSuccess() {
		// TODO Auto-generated method stub
		if (network_error_bg.getVisibility() == View.VISIBLE) {
			network_error_bg.setVisibility(View.INVISIBLE);
			content.setVisibility(View.VISIBLE);
		}
		if (network_message.getVisibility() == View.VISIBLE) {
			network_message.setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && mSlidingMenu.isMenuShowing()) {
			showContent();
			return true;
		} else {
			final int backCount = getSupportFragmentManager().getBackStackEntryCount();
			if (backCount == 0) {
				Intent i = new Intent();
				i.setAction(Intent.ACTION_MAIN);
				i.addCategory(Intent.CATEGORY_HOME);
				startActivity(i);
				overridePendingTransition(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_right_exit);
			} else {
				super.onBackPressed();

			}
		}
		return false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == VHConstants.REQUEST_CODE_SEARCH && resultCode == RESULT_OK) {
			Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.content);
			if (fragment != null) {
				// getSupportFragmentManager().beginTransaction().detach(fragment).attach(fragment).commit();
				if (fragment instanceof VHSearchResultFragment) {
					((VHCommonFragment) fragment).onDataRefresh();
				} else {

					replaceFragmentNoBackStackWithAnimation(VHSearchResultFragment.newInstance(data.getStringExtra(VHConstants.EXTRA_KEY_WORD)));
				}
			}
		}

	}
}
